<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow=true" type="primary" round plain>新增</el-button>
    </div>
    <el-table :data="goodsList">
      <el-table-column label="序号" prop="id" min-width="80"></el-table-column>
      <el-table-column
        label="一级分类"
        prop="firstcatename"
        min-width="180"
      ></el-table-column>
      <el-table-column
        label="二级分类"
        prop="secondcatename"
        min-width="180"
      ></el-table-column>
      <el-table-column
        label="商品名称"
        prop="goodsname"
        min-width="180"
      ></el-table-column>
      <el-table-column label="商品图片" prop="img" min-width="180">
        <template v-slot="props">
          <img :src="$baseUrl + props.row.img" style="height: 80px" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="商品价格" prop="price" min-width="180">
        <template v-slot="props">
          <b style="color: red">&yen;{{ props.row.price }}</b>
        </template>
      </el-table-column>
      <el-table-column label="市场价格" prop="market_price" min-width="180">
        <template v-slot="props">
          <b style="color: red">&yen;{{ props.row.market_price }}</b>
        </template>
      </el-table-column>
      <el-table-column label="是否新品" prop="isnew" min-width="180">
        <template v-slot="props">
          <el-tooltip
            :content="props.row.isnew === 1 ? '是' : '否'"
            placement="top"
          >
            <el-switch
              v-model="props.row.isnew"
              :active-value="1"
              :inactive-value="0"
              @change="updateGoods(props.row)"
            >
            </el-switch>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="是否热卖" prop="ishot" min-width="180">
        <template v-slot="props">
          <el-tooltip
            :content="props.row.ishot === 1 ? '是' : '否'"
            placement="left"
          >
            <el-switch
              v-model="props.row.ishot"
              :active-value="1"
              :inactive-value="0"
               @change="updateGoods(props.row)"
            >
            </el-switch>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="商品状态" prop="status" min-width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="mini"
            round
            plain
            v-if="props.row.status === 1"
            >正常</el-button
          >
          <el-button type="danger" size="mini" round plain v-else
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="180" fixed="right">
        <template v-slot="props">
          <el-button @click="edit(props.row)" type="success" size="mini" plain round>编辑</el-button>
        <el-button @click="del(props.row.id)" type="danger" size="mini" plain round>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :page-sizes="[5,10,15,20]"
      :total="goodsTotal"
      :page-size="size"
      layout="prev,pager,next,sizes"
      background
      @current-change="pageChange"
      @size-change="sizeChange"
    ></el-pagination>
    <!-- 调用子组件 -->
    <Dialog ref="dialog" :info="info" @updateGoods="updateGoodsHandle"/>
  </div>
</template>

<script>
// 导入对画框组件
import Dialog from './Dialog';
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  components:{Dialog},
  data() {
    return {
      page: 1, //页码
      size: 5, //每页显示数据条数
      info:{
        // 控制对话框组件的显示状态
        isShow:false
      }
    };
  },
  computed: {
    ...mapState("goods", ["goodsList", "goodsTotal"]),
  },
  methods: {
    ...mapActions("goods", ["getGoodsListAction", "getGoodsTotalAction"]),
    // 删除商品
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/goodsdelete',{id}).then(res=>{
          if(res.code===200){
            this.$message.success(res.msg);
            // 重新请求商品列表和商品总数
            this.updateGoodsHandle();
          }else{
            this.$message.error(res.msg);
          }
        });
      })
    },
    // 监听每页显示数据条数size的变化
    sizeChange(size){
      // 1-更新每页显示数据条数
      this.size=size
      // 2-根据最新的size, 请求数据 
      this.getGoodsListAction({page:this.page,size:this.size});
    },
    // 编辑按钮点击事件处理函数
    edit(row){
      // 1-显示对话框组件
      this.info.isShow=true;
      // 2-将待编辑的商品信息传递给对话框组件
      this.$refs.dialog.setFormData(row);
    },
    // 自定义事件updateGoods的处理函数
    updateGoodsHandle(){
        this.getGoodsListAction({ page: this.page, size: this.size });
        this.getGoodsTotalAction();
    },
    // 更新商品状态
    updateGoods(row){
        const {id,isnew,ishot}=row;
        // 创建FormDats实例对象
        const fd=new FormData();
        fd.append('id',id);
        fd.append('isnew',isnew);
        fd.append('ishot',ishot);
        this.$axios.post('/goodsedit',fd).then(res=>{
            if(res.code===200){
                this.$message.success(res.msg);
            }else{
                this.$message.error(res.msg);
            }
        });
    },
    // 分页按钮点击事件处理函数
    pageChange(page){
        // 1-更新分页页码值
        this.page=page;
        // 2-根据最新的页码请求数据
        this.getGoodsListAction({page:this.page,size:this.size});
    }
  },
  created() {
    this.getGoodsListAction({ page: this.page, size: this.size });
    this.getGoodsTotalAction();
  },
};
</script>

<style>
</style>